package com.ruoyi.main.controller;

import java.util.List;

import com.ruoyi.main.service.IMainUnitesService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.main.domain.MainProduct;
import com.ruoyi.main.service.IMainProductService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 产品管理Controller
 * 
 * @author chao
 * @date 2024-07-23
 */
@Controller
@RequestMapping("/main/product")
public class MainProductController extends BaseController
{
    private String prefix = "main/product";

    @Autowired
    private IMainProductService mainProductService;


    @RequiresPermissions("main:product:view")
    @GetMapping()
    public String product()
    {
        return prefix + "/product";
    }

    /**
     * 查询产品管理列表
     */
    @RequiresPermissions("main:product:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(MainProduct mainProduct)
    {
        startPage();
        List<MainProduct> list = mainProductService.selectMainProductList(mainProduct);
        return getDataTable(list);
    }

    /**
     * 导出产品管理列表
     */
    @RequiresPermissions("main:product:export")
    @Log(title = "产品管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(MainProduct mainProduct)
    {
        List<MainProduct> list = mainProductService.selectMainProductList(mainProduct);
        ExcelUtil<MainProduct> util = new ExcelUtil<MainProduct>(MainProduct.class);
        return util.exportExcel(list, "产品管理数据");
    }

    /**
     * 新增产品管理
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存产品管理
     */
    @RequiresPermissions("main:product:add")
    @Log(title = "产品管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(MainProduct mainProduct)
    {
        return toAjax(mainProductService.insertMainProduct(mainProduct));
    }

    /**
     * 修改产品管理
     */
    @RequiresPermissions("main:product:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        MainProduct mainProduct = mainProductService.selectMainProductById(id);
        mmap.put("mainProduct", mainProduct);
        return prefix + "/edit";
    }

    /**
     * 修改保存产品管理
     */
    @RequiresPermissions("main:product:edit")
    @Log(title = "产品管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(MainProduct mainProduct)
    {
        return toAjax(mainProductService.updateMainProduct(mainProduct));
    }

    /**
     * 删除产品管理
     */
    @RequiresPermissions("main:product:remove")
    @Log(title = "产品管理", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(mainProductService.deleteMainProductByIds(ids));
    }
}
